﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace SurveillanceLib
{
    /// <summary>
    /// 树的操作
    /// </summary>
    public class TreeViewOperation
    {
        #region 属性

        /// <summary>
        /// 根节点
        /// </summary>
        public TreeView TreeViewRoot { get; set; }

        /// <summary>
        /// 设备断开连接处理
        /// </summary>
        public Action<int, StringBuilder, int, IntPtr> DisConnectAction { get; set; }

        #endregion

        #region 获得数据库信息放入 TreeView 中(递归)

        /// <summary>
        /// 获得数据库信息放入 TreeView 中(递归)
        /// </summary>
        /// <param name="parentID"></param>
        /// <param name="parentNode"></param>
        public void AddTreeView(int parentID, TreeNode parentNode)
        {
            try
            {
                SurveillanceDAL dataOper = new SurveillanceDAL();
                DataView dataTree = dataOper.GetDataSet(string.Format(SurveillanceDAL.QUERY_GET_TREENODEDATA_BYSTATUS, 1),
                    string.Empty, "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName)).Tables[0].DefaultView;
                dataTree.RowFilter = string.Format("ParentID = {0}", parentID);

                foreach (DataRowView row in dataTree)
                {
                    TagModel tag = new TagModel()
                    {
                        Driver_IsLogin = false,
                        Driver_ExceptChannel = row["ExceptChannel"].ToString(),
                        Driver_IP = row["TreeName"].ToString(),
                        Driver_Port = row["DriverPort"].ToString(),
                        Driver_User = row["DriverName"].ToString(),
                        Driver_Password = row["DriverPwd"].ToString(),
                        Driver_Type = row["DriverType"].ToString(),
                        Driver_Instance = DriverFactory.Factory.CreateDriver(row["DriverType"].ToString()),
                    };

                    TreeNode node = new TreeNode()
                    {
                        Name = row["ID"].ToString(),
                        Text = row["TreeText"].ToString(),
                        ToolTipText = string.IsNullOrWhiteSpace(row["DriverPort"].ToString()) ?
                            string.Format("{0}", row["TreeText"].ToString()) :
                            string.Format("{0}[{1}:{2}]", row["TreeText"].ToString(),
                                row["TreeName"].ToString(), row["DriverPort"].ToString()),
                        Tag = tag,
                    };

                    if (tag != null && tag.Driver_Instance != null && 
                        this.DisConnectAction != null)
                    { 
                        tag.Driver_Instance.InitializeData(this.DisConnectAction, IntPtr.Zero); // 初始化状态
                        tag.Driver_Instance.SetEncoding("gb2312"); // 字符编码格式设置，默认为 gb2312 字符编码
                    }

                    if (parentNode == null) // 处理根节点
                        TreeViewRoot.Nodes.Add(node);
                    else // 处理子节点
                        parentNode.Nodes.Add(node);

                    AddTreeView(Int32.Parse(row["ID"].ToString().Trim()), node);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}

